package com.elm.controller;


import cn.hutool.db.sql.Order;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.elm.common.R;
import com.elm.dao.CommentsDao;
import com.elm.dao.OrdersDao;
import com.elm.dao.StoreDao;
import com.elm.dao.UserDao;
import com.elm.dao.requestData.CommentsSearchData;
import com.elm.dao.requestData.OrderItemsData;
import com.elm.dao.responseData.CommentsResData;
import com.elm.domain.Comments;
import com.elm.domain.Orders;
import com.elm.domain.Store;
import com.elm.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@ResponseBody
@RequestMapping("/api/comments")
public class CommentsController {

    @Autowired
    private UserDao userDao;

    @Autowired
    private OrdersDao ordersDao;

    @Autowired
    private CommentsDao commentsDao;

    @Autowired
    private StoreDao storeDao;

    @PostMapping("/store")
    public R getStoreComments(@RequestBody CommentsSearchData commentsSearchData)
    {
        //得到商家的相关评论
//        System.out.println("获得了---------"+commentsSearchData.getStoreId());
        QueryWrapper<Comments> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("store_id",commentsSearchData.getStoreId())
                .eq("flag",0)
                .eq(commentsSearchData.getStar()!=0,"star",commentsSearchData.getStar())
                .orderBy(true,false,"time")
                .like(!commentsSearchData.getContent().equals(""),"content",commentsSearchData.getContent());

        IPage<Comments> iPage=new Page<>();
//        iPage.setCurrent(commentsSearchData.getCurrent());
        iPage.setSize(5);

        IPage<Comments> res = commentsDao.selectPage(iPage, queryWrapper);

        List<CommentsResData> list=new ArrayList<>();
        for (Comments comments:res.getRecords())
        {
            comments.setImagesList(JSON.parseArray(comments.getImages(),String.class));
            CommentsResData commentsResData=new CommentsResData();
            //寻找有没有回复
            QueryWrapper<Comments> commentsQueryWrapper=new QueryWrapper<>();
            commentsQueryWrapper.eq("store_id",comments.getStoreId())
                    .eq("orders_id",comments.getOrdersId())
                    .eq("flag",1)
                    .eq("sender_id",comments.getReceiverId())
                    .eq("receiver_id",comments.getSenderId());

            Comments response = commentsDao.selectOne(commentsQueryWrapper);
            if(response!=null)
            {
                comments.setStoreResponse(response.getContent());
            }

            QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
            userQueryWrapper.select("nickname","avatar")
                    .eq("id",comments.getSenderId());

            User user = userDao.selectOne(userQueryWrapper);

            QueryWrapper<Orders> ordersQueryWrapper=new QueryWrapper<>();
            ordersQueryWrapper.select("orderInfo")
                            .eq("id",comments.getOrdersId());

            Orders orders = ordersDao.selectOne(ordersQueryWrapper);
            orders.setItemsLists(JSON.parseArray(orders.getOrderInfo(), OrderItemsData.class));

            commentsResData.setComments(comments);
            commentsResData.setUser(user);
            commentsResData.setOrders(orders);

            list.add(commentsResData);
//            commentsResData.setUser(u);
        }

        return R.success(list);
    }

    @PostMapping("/user")
    public R getUserComments(@RequestBody CommentsSearchData commentsSearchData) {
        QueryWrapper<Comments> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sender_id", commentsSearchData.getUserId())
                .eq("flag", 0)
                .eq(commentsSearchData.getStar() != 0, "star", commentsSearchData.getStar())
                .orderBy(true, false, "time")
                .like(!commentsSearchData.getContent().equals(""), "content", commentsSearchData.getContent());

        IPage<Comments> iPage = new Page<>();
//        iPage.setCurrent(commentsSearchData.getCurrent());
        iPage.setSize(5);

        IPage<Comments> res = commentsDao.selectPage(iPage, queryWrapper);

        List<CommentsResData> list = new ArrayList<>();
        for (Comments comments : res.getRecords()) {
            comments.setImagesList(JSON.parseArray(comments.getImages(), String.class));
            CommentsResData commentsResData = new CommentsResData();
            //寻找有没有回复
            QueryWrapper<Comments> commentsQueryWrapper=new QueryWrapper<>();
            commentsQueryWrapper.eq("store_id",comments.getStoreId())
                    .eq("orders_id",comments.getOrdersId())
                    .eq("flag",1)
                    .eq("sender_id",comments.getReceiverId())
                    .eq("receiver_id",comments.getSenderId());

            Comments response = commentsDao.selectOne(commentsQueryWrapper);
            if(response!=null)
            {
                comments.setStoreResponse(response.getContent());
            }

            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.select("nickname", "avatar")
                    .eq("id", comments.getSenderId());

            User user = userDao.selectOne(userQueryWrapper);

            QueryWrapper<Orders> ordersQueryWrapper = new QueryWrapper<>();
            ordersQueryWrapper.select("orderInfo")
                    .eq("id", comments.getOrdersId());

            Orders orders = ordersDao.selectOne(ordersQueryWrapper);
            orders.setItemsLists(JSON.parseArray(orders.getOrderInfo(), OrderItemsData.class));

            Store store = storeDao.selectById(comments.getStoreId());

            commentsResData.setComments(comments);
            commentsResData.setUser(user);
            commentsResData.setOrders(orders);
            commentsResData.setStore(store);

            list.add(commentsResData);
//            commentsResData.setUser(u);
        }
        return R.success(list).add("page",res);
    }

}
